package com.bing.bean.Tpp.stock.func;

import com.bing.Component.Context;
import com.bing.bean.weixin.ResponseMessage;
import com.bing.services.impl.AbstractCommandService;
import com.bing.utils.MsgHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.StringUtils;

import java.util.*;

/**
 * Created by Administrator on 2015/5/31.
 */
public class StockCommandService extends AbstractCommandService {
    protected int order = 101;
    private static Logger logger = LoggerFactory.getLogger(StockCommandService.class);
    private static final String TWGP = TW + "股票";
    private static final String TWDM = TW + "股票" + TW + "代码";
    private static List<String> contexts = new ArrayList<String>() {
        {
            add(TWGP);
            add(TWDM);
        }
    };

    private static Map<String, Map<String, String>> userCommands = new HashMap<>();

    @Override
    protected List<String> getContexts() {
        return contexts;
    }

    @Override
    public ResponseMessage handle(Context context) {
        String content;

        switch (context.getCommand()) {
            //顶级context ;
            case TWGP:
                switch (context.getRequestMessage().getMsgType()) {
                    case text:
                        content = context.getRequestMessage().getContent();
                        if (!StringUtils.hasText(content) || "?".equals(context.getCommand()) || "？".equals(context.getCommand())) {
                            return MsgHelper.toText(context.getRequestMessage(), "? 帮助");
                        }

                        Map<String, String> menu = new HashMap<>();
                        menu.put("1", "sh600000");
                        menu.put("sh600000", "sh600000");
                        menu.put("2", "sh600001");
                        menu.put("sh600001", "sh600001");
                        menu.put("?", "帮助");

                        userCommands.put(context.getUser().getOpenId(), menu);
                        context.getUser().setCommand(TWDM);
                        context.getUser().setHistoryCommands("?");
                        context.getUser().setLatestActive(new Date());
                        userService.updateUser(context.getUser());
                        return MsgHelper.toText(context.getRequestMessage(), "? 帮助 1,2");

                        /*
                            try {
                                List<Stock> stocks = StockUtil.gets(new String[]{context.getCommand()});
                                return MsgHelper.toText(context.getRequestMessage(), stocks.get(0).toString());
                            } catch (IOException e) {
                                return MsgHelper.toText(context.getRequestMessage(), "网络异常! 稍后再查");
                            } catch (BingException e) {
                                return MsgHelper.toText(context.getRequestMessage(), e.getErrorMsg());
                            }*/
                    default:

                        break;
                }
                break;

            case TWDM:
                switch (context.getRequestMessage().getMsgType()) {
                    case text:
                        content = context.getRequestMessage().getContent();
                        content = userCommands.get(context.getUser().getOpenId()).get(content);
                        if (!StringUtils.hasText(content) || "?".equals(context.getCommand()) || "？".equals(context.getCommand())) {
                            return MsgHelper.toText(context.getRequestMessage(), userCommands.get(context.getUser().getOpenId()).get("?"));
                        }
                        logger.info("查找股票代码: {}", content);
                        userCommands.remove(context.getUser().getOpenId());
                        context.getUser().setCommand(TWDM.substring(0, TWDM.lastIndexOf(TW)));
                        context.getUser().setLatestActive(new Date());
                        userService.updateUser(context.getUser());
                        return MsgHelper.toText(context.getRequestMessage(), "查询成功");
                    default:
                        return MsgHelper.toText(context.getRequestMessage(), "默认返回");
                }

            default:
                break;
        }


        return null;
    }

    @Override
    protected Logger getLogger() {
        return logger;
    }

}
